package com.sise.studentInfoSystem.filter;


import com.sise.studentInfoSystem.domain.TStudent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Component
@WebFilter(filterName = "roleFilter",urlPatterns = "/stuInfoSystem/*")
public class RoleFilter implements Filter {

    //定义只允许学生访问的url
    private String studentAllowUrl[] = {"toIndex","toInfo","toBehavior","toAcademic","toCreativeAbility","toProfessionalAbility","toOrganizationManagement","toSocialPractice","toStylisticAbility"};
    //定义只允许测评小组访问的url
    private String stuSurveyAllowUrl[] = {"toSurveyInfo","toSurveyBehavior","toSurveyAcademic","toSurveyProfessionalAbility","toSurveyCreativeAbility","toSurveyOrganizationManagement","toSurveyStylisticAbility","toSurveySocialPractice"};
    //定义只允许老师访问的url
    private String teacherAllowUrl[] = {"ss"};
    //定义只允许超级管理员访问的url
    private String superUserAllowUrl[] = {"sf"};
    //定义静态资源后缀数组
    private String staticUrl[] = {".css",".jpg",".png"};

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        //获取访问的url
        String requestUrl = request.getRequestURI();
        String lastUrl = requestUrl.substring(requestUrl.lastIndexOf('/')+1);


        int num = 1;
        for (String s : studentAllowUrl) {
            //判断url是否为学生专属访问
            if ( lastUrl.equals(s)){
                num++;
                //获取session的loginStu,判断是否存在
                TStudent student = (TStudent) session.getAttribute("loginStu");
                if (student == null){
                    //重定向到学生登录界面
                    response.sendRedirect("/stuInfoSystem/");
                }else {
                    //放行
                    filterChain.doFilter(servletRequest,servletResponse);
                }
                break;
            }
        }
        for (String s : stuSurveyAllowUrl) {
            //判断url是否为测评小组专属访问
//            if ( lastUrl.equals(s)){
//                num++;
//                //获取session的loginStu,判断是否存在
//                TStudent student = (TStudent) session.getAttribute("loginStu");
//                if (student == null){
//                    //重定向到测评人员登录界面
//                    response.sendRedirect("/stuInfoSystem/");
//                }else {
//                    //放行
//                    filterChain.doFilter(servletRequest,servletResponse);
//                }
//                break;
//            }

        }

        for (String s : teacherAllowUrl) {
            //判断url是否为老师专属访问
//            if ( lastUrl.equals(s)){
//                num++;
//                //获取session的属性**,判断是否存在
//                TStudent student = (TStudent) session.getAttribute("loginStu");
//                if (student == null){
//                    //重定向到老师登录界面
//                    response.sendRedirect("/stuInfoSystem/");
//                }else {
//                    //放行
//                    filterChain.doFilter(servletRequest,servletResponse);
//                }
//                break;
//            }
        }

        for (String s : superUserAllowUrl) {
            //判断url是否为超级管理员专属访问
//            if ( lastUrl.equals(s)){
//                num++;
//                //获取session的属性***,判断是否存在
//                TStudent student = (TStudent) session.getAttribute("loginStu");
//                if (student == null){
//                    //重定向到超级管理员登录界面
//                    response.sendRedirect("/stuInfoSystem/");
//                }else {
//                    //放行
//                    filterChain.doFilter(servletRequest,servletResponse);
//                }
//                break;
//            }
        }
        //
        if (num ==1)
            filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {

    }
}
